package com.example.smartcity_ikun3

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import com.example.smartcity_ikun3.logic.model.LoginModel
import com.example.smartcity_ikun3.logic.model.newLogin
import com.example.smartcity_ikun3.logic.network.RetrofitService.Companion.create
import com.example.smartcity_ikun3.logic.network.SmartApi
import com.example.smartcity_ikun3.logic.util.Util.show
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class NewLoginActivity : AppCompatActivity() {

    private lateinit var mEdText: EditText
    private lateinit var mBtnOk: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_new_login)
        title = "修改信息"
        mEdText = findViewById(R.id.newLoginEdText)
        mBtnOk = findViewById(R.id.newLoginBtn)

        val name = intent.getStringExtra("name")
        val sex = intent.getStringExtra("sex")
        val phone = intent.getStringExtra("phone")
        val userId = intent.getStringExtra("id")
        name?.let { mEdText.setText(name) }
        sex?.let { mEdText.setText(sex) }
        phone?.let { mEdText.setText(phone) }
        userId?.let { mEdText.setText(userId) }

        mBtnOk.setOnClickListener {

            /**
             * Name
             */
            name?.let {
                getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    val token = getString("token", "").toString()
                    create<SmartApi>().getNewLogin(token, RequestBody.create("application/json".toMediaTypeOrNull(), JSONObject().apply {
                        put("nickName", mEdText.text.toString())
                    }.toString())).enqueue(object : Callback<newLogin>{
                        override fun onFailure(p0: Call<newLogin>, p1: Throwable) {
                            "修改失败！".show()
                            Log.d("NET", "onFailure: newLogin --> ${p1.message}")
                        }

                        override fun onResponse(p0: Call<newLogin>, p1: Response<newLogin>) {
                            val body = p1.body()
                            if (body != null){
                                if (body.code == 200){
                                    "修改成功".show()
                                }else "修改失败！".show()
                            }
                        }
                    })
                }
            }

            /**
             * Sex
             */
            sex?.let {
                getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    val token = getString("token", "").toString()
                    create<SmartApi>().getNewLogin(token, RequestBody.create("application/json".toMediaTypeOrNull(), JSONObject().apply {
                        if (mEdText.text.toString() == "男") put("sex", "0")
                        if (mEdText.text.toString() == "女") put("sex", "1")
                    }.toString())).enqueue(object : Callback<newLogin>{
                        override fun onFailure(p0: Call<newLogin>, p1: Throwable) {
                            "修改失败！".show()
                            Log.d("NET", "onFailure: newLogin --> ${p1.message}")
                        }

                        override fun onResponse(p0: Call<newLogin>, p1: Response<newLogin>) {
                            val body = p1.body()
                            if (body != null){
                                if (body.code == 200){
                                    "修改成功".show()
                                }else "修改失败！".show()
                            }
                        }
                    })
                }
            }


            /**
             * Phone
             */
            phone?.let {
                getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    val token = getString("token", "").toString()
                    create<SmartApi>().getNewLogin(token, RequestBody.create("application/json".toMediaTypeOrNull(), JSONObject().apply {
                        put("phonenumber", mEdText.text.toString())
                    }.toString())).enqueue(object : Callback<newLogin>{
                        override fun onFailure(p0: Call<newLogin>, p1: Throwable) {
                            "修改失败！".show()
                            Log.d("NET", "onFailure: newLogin --> ${p1.message}")
                        }

                        override fun onResponse(p0: Call<newLogin>, p1: Response<newLogin>) {
                            val body = p1.body()
                            if (body != null){
                                if (body.code == 200){
                                    "修改成功".show()
                                }else "修改失败！".show()
                            }
                        }
                    })
                }
            }


            /**
             * UserId
             */
            userId?.let {
                getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    val token = getString("token", "").toString()
                    create<SmartApi>().getNewLogin(token, RequestBody.create("application/json".toMediaTypeOrNull(), JSONObject().apply {
                        put("idCard", mEdText.text.toString())
                    }.toString())).enqueue(object : Callback<newLogin>{
                        override fun onFailure(p0: Call<newLogin>, p1: Throwable) {
                            "修改失败！".show()
                            Log.d("NET", "onFailure: newLogin --> ${p1.message}")
                        }

                        override fun onResponse(p0: Call<newLogin>, p1: Response<newLogin>) {
                            val body = p1.body()
                            if (body != null){
                                if (body.code == 200){
                                    "修改成功".show()
                                }else "修改失败！".show()
                            }
                        }
                    })
                }
            }


        }


    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}